package com.jyniubi.core.controller.admin;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyniubi.base.exception.Assert;
import com.jyniubi.base.result.R;
import com.jyniubi.base.result.ResponseEnum;
import com.jyniubi.core.pojo.entity.UserInfo;
import com.jyniubi.core.pojo.vo.UserInfoSearchVO;
import com.jyniubi.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author jyzhang
 * @since 2024-04-27
 */
@Api(tags = "会员模块")
@RestController

@RequestMapping("/admin/core/userInfo")
public class AdminUserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @ApiOperation("查询所有用户")
    @GetMapping
    public R listAll(){
        List<UserInfo> userInfos = userInfoService.list();
        return R.ok().data("items",userInfos);
    }

    @ApiOperation("分页查询用户")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R list(UserInfoSearchVO userInfoSearchVO,
                  @PathVariable("pageNum") Integer pageNum,
                  @PathVariable("pageSize") Integer pageSize){
        Page<UserInfo> page = userInfoService.listPage(userInfoSearchVO,pageNum,pageSize);
        return R.ok().data("page",page);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id){
        UserInfo userInfo = userInfoService.getById(id);
        return R.ok().data("item",userInfo);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id){
        boolean b = userInfoService.removeById(id);
        Assert.assertNotTrue(b, ResponseEnum.DELETE_USERINFO_FALSE);
        return R.ok();
    }

    @ApiOperation("锁定解锁用户状态")
    @PutMapping("/lock/{id}/{status}")
    public R lock(@PathVariable("id") String id,@PathVariable("status") Integer status){
        userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class).
                eq(UserInfo::getId,id).
                set(UserInfo::getStatus,status));
        return R.ok();
    }

}

